package com.cee.admin.common.utils;

import com.cee.common.exception.GlobalException;
import com.cee.common.web.result.ResultCode;
import com.cee.domain.modules.sys.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * @author wsz
 * @since 2024/1/16
 */
public class SysUserUtils {

    private SysUserUtils() {}

    /**
     * 获取currentUser
     * @return sysFrontUsers
     * @throws NullPointerException 当subject为null时
     */
    public static User currentUser() {
        Subject subject = SecurityUtils.getSubject();
        if (subject == null) {
            throw new GlobalException(ResultCode.NOT_LOGIN);
        }
        return (User) subject.getPrincipal();
    }

    public static Long currentUserId() {
        User user = currentUser();
        if (user == null) {
            throw new GlobalException(ResultCode.NOT_LOGIN);
        }
        return user.getId();
    }
}
